package io.renren.modules.manage.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import io.renren.modules.manage.entity.BannerEntity;
import io.renren.modules.manage.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.manage.entity.ProductCategoryEntity;
import io.renren.modules.manage.service.ProductCategoryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author hyc
 * @email 2250242464qq.com
 * @date 2024-03-08 23:09:55
 */
@RestController
@RequestMapping("manage/productcategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private UploadService uploadService;
    @Value("${uploads.path}")
    private String uploadDir;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("manage:productcategory:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = productCategoryService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/queryAllCategory")
    //@RequiresPermissions("manage:productcategory:list")
    public R queryAllCategory(){
        List<ProductCategoryEntity> list = productCategoryService.list();
        return R.ok().put("categorys", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("manage:productcategory:info")
    public R info(@PathVariable("id") Integer id){
		ProductCategoryEntity productCategory = productCategoryService.getById(id);

        return R.ok().put("productCategory", productCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("manage:productcategory:save")
    public R save(@RequestBody ProductCategoryEntity productCategory){
		productCategoryService.save(productCategory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("manage:productcategory:update")
    public R update(@RequestBody ProductCategoryEntity productCategory){
		productCategoryService.updateById(productCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("manage:productcategory:delete")
    public R delete(@RequestBody Integer[] ids){
        String type = "category";
        Arrays.asList(ids).forEach(id -> {
            ProductCategoryEntity categoryEntity = productCategoryService.getById(id);
            String[] split = categoryEntity.getImgUrl().split(UploadController.SEPARATOR);
            uploadService.deleteFile(uploadDir, type, split[2]);
        });
		productCategoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
